package com.gh.controller;

import com.gh.model.cmn.Dict;
import com.gh.result.Result;
import com.gh.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author Eric
 * @Date 2021/6/5 23:56
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;
    /**
     * 根据数据id查询子数据列表
     */
    @GetMapping("getChildDictListById/{id}")
    public Result getDictListById(@PathVariable Long id){
        List<Dict> list = dictService.findListById(id);
        return Result.ok(list);
    }
    /**
     * 导出数据字典到Excel
     */
    @GetMapping("exportDictToExcel")
    public Result exportDictToExcel(HttpServletResponse response){
        dictService.exportDictToExcel(response);
        return Result.ok();
    }
    /**
     * 从Excel导入数据到数据库
     */
    @PostMapping("importDictToDatabase")
    public Result importDictToDatabase(MultipartFile file){
        dictService.importDictToDatabase(file);
        return Result.ok();
    }
    /**
     * 根据value值查询
     */
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        String name = dictService.getDictName("",value);
        return name;
    }
    /**
     * 根据dict_code和value值查询
     */
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){
        String name = dictService.getDictName(dictCode, value);
        return name;
    }
    /**
     * 根据dictCode获取下级结点
     */
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
